package display;

import javax.swing.*;
import java.awt.*;
import java.io.FileInputStream;
import java.io.InputStream;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;

import  java.io.*;

public class Menu extends JPanel {
	
  Image bgImage=null;
  String message="";
  ImageIcon icon1 = new ImageIcon("resource/graphics/menu/playerButton.gif");
  ImageIcon icon2 = new ImageIcon("resource/graphics/menu/introButton.gif");
  ImageIcon icon3 = new ImageIcon("resource/graphics/menu/newLevButton.gif");
  ImageIcon icon4 = new ImageIcon("resource/graphics/menu/musicButton.gif");
  ImageIcon icon5 = new ImageIcon("resource/graphics/menu/web.gif");
  ImageIcon icon6 = new ImageIcon("resource/graphics/menu/star.png");
    JButton jButton1 = new JButton(icon1);
    JButton jButton2 = new JButton(icon2);
    JButton jButton3 = new JButton(icon3);
    JButton jButton4 = new JButton(icon4);
    JButton jButton5 = new JButton(icon5);
    JButton jButton6 = new JButton(icon6);
    public Menu() {
    super(null);
    try {
      getFileImage("resource/graphics/menu/menuMain.gif");
    }
    catch (Exception ex) {
      message="File load failed: "+ex.getMessage();
    }
        try {
            jbInit();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
  public void paintComponent(Graphics g) {
    if (bgImage!=null) {
      g.drawImage(bgImage,0,0,this);
    } else {
      g.drawString(message,40,40);
    }
  }
  private void getFileImage(String filePath) throws InterruptedException, IOException {
    FileInputStream in = new FileInputStream(filePath);
    byte [] b=new byte[in.available()];
    in.read(b);
    in.close();
    bgImage=Toolkit.getDefaultToolkit().createImage(b);
    MediaTracker mt=new MediaTracker(this);
    mt.addImage(bgImage,0);
    mt.waitForAll();
  }
  public static void main(String[] args) {
    Menu bgd=new Menu();
    JFrame f = new JFrame("Welcome to Lemmings");
    f.add(bgd);
    f.setSize(320, 200);
    f.setVisible(true);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(320, 200);
  }

    private void jbInit() throws Exception {
        this.setLayout(null);
        jButton1.setBounds(new Rectangle(20, 167, 90, 110));
        jButton1.addActionListener(new panelMain_jButton1_actionAdapter(this));
        jButton2.setBounds(new Rectangle(140, 167, 90, 110));
        jButton2.addActionListener(new panelMain_jButton2_actionAdapter(this));
        jButton3.setBounds(new Rectangle(260, 167, 90, 110));
        jButton3.addActionListener(new panelMain_jButton3_actionAdapter(this));
        jButton4.setBounds(new Rectangle(380, 167, 90, 110));
        jButton4.addActionListener(new panelMain_jButton4_actionAdapter(this));
        jButton5.setBounds(new Rectangle(485, 225, 50, 50));
        jButton5.addActionListener(new panelMain_jButton5_actionAdapter(this));
        jButton5.setToolTipText("Visit TeamLemmings website");
        jButton6.setBounds(new Rectangle(485, 167, 50, 50));
        jButton6.addActionListener(new panelMain_jButton6_actionAdapter(this));
        jButton6.setToolTipText("Upload high score");
        this.add(jButton1);
        this.add(jButton2);
        this.add(jButton3);
        this.add(jButton4);
        this.add(jButton5);
        this.add(jButton6);
        try{
            InputStream in = new FileInputStream("resource/sound/music/Music/tune.mid");
            AudioStream as = new AudioStream(in); 
        	AudioPlayer.player.start(as);            
            }catch(Exception o){
            	System.out.println("File could not be loaded");
            }
    }

    public void jButton1_actionPerformed(ActionEvent e) {
        JOptionPane.showMessageDialog(null, "Button1!!");
    }

    public void jButton2_actionPerformed(ActionEvent e) {
    	JOptionPane.showMessageDialog(null, "Button2!!");
    }

    public void jButton3_actionPerformed(ActionEvent e) {
    	JOptionPane.showMessageDialog(null, "Button3!!");
    	//Game game = new Game();
	
    }

    public void jButton4_actionPerformed(ActionEvent e) {
    	JOptionPane.showMessageDialog(null, "Button4!!");
    }
    public void jButton5_actionPerformed(ActionEvent e) {
    	String homePage = "http://teamlemmings.5gbfree.com/";
    	BrowserLaunch.openURL(homePage.trim());
    }
    public void jButton6_actionPerformed(ActionEvent e) {
    	String name = JOptionPane.showInputDialog(null, "Name");
    	String score = JOptionPane.showInputDialog(null, "Score");
    	Connection connection = null; 
    	try { 
//    	 Load the JDBC driver 
    	Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //Or any other driver
//    	 Create a connection to the database 
    	String serverName = "5gbfree.com "; 
    	String mydatabase = "teamlemming_score"; 
    	String url = "jdbc:mysql://" + serverName + "/" + mydatabase; // a JDBC url 
    	String username = "teamlemmings_score@localhost "; 
    	String password = "score"; 
    	connection = DriverManager.getConnection(url, username, password);
    	Statement stmt = connection.createStatement();
    	stmt.executeUpdate("INSERT INTO scores " + 
    	   "VALUES (Test2, 2)");
    	connection.close();
    	} catch (ClassNotFoundException f) { 
//    	 Could not find the database driver 
    	} catch (SQLException f) { 
//    	 Could not connect to the database 
    	} 


    }
}
class panelMain_jButton6_actionAdapter implements ActionListener {
    private Menu adaptee;
    panelMain_jButton6_actionAdapter(Menu adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.jButton6_actionPerformed(e);
    }
}

class panelMain_jButton5_actionAdapter implements ActionListener {
    private Menu adaptee;
    panelMain_jButton5_actionAdapter(Menu adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.jButton5_actionPerformed(e);
    }
}

class panelMain_jButton4_actionAdapter implements ActionListener {
    private Menu adaptee;
    panelMain_jButton4_actionAdapter(Menu adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.jButton4_actionPerformed(e);
    }
}


class panelMain_jButton3_actionAdapter implements ActionListener {
    private Menu adaptee;
    panelMain_jButton3_actionAdapter(Menu adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.jButton3_actionPerformed(e);
    }
}


class panelMain_jButton2_actionAdapter implements ActionListener {
    private Menu adaptee;
    panelMain_jButton2_actionAdapter(Menu adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.jButton2_actionPerformed(e);
    }
}


class panelMain_jButton1_actionAdapter implements ActionListener {
    private Menu adaptee;
    panelMain_jButton1_actionAdapter(Menu adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.jButton1_actionPerformed(e);
    }
    
}

class BrowserLaunch {

	   private static final String errMsg = "Error attempting to launch web browser";

	   public static void openURL(String url) {
	      String osName = System.getProperty("os.name");
	      try {
	         if (osName.startsWith("Mac OS")) {//check for mac os
	            Class fileMgr = Class.forName("com.apple.eio.FileManager");
	            Method openURL = fileMgr.getDeclaredMethod("openURL",
	               new Class[] {String.class});
	            openURL.invoke(null, new Object[] {url});
	            }
	         else if (osName.startsWith("Windows"))//check if windows
	            Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
	         else { //check for Unix or Linux
	            String[] browsers = {
	               "firefox", "opera", "konqueror", "epiphany", "mozilla", "netscape" };
	            String browser = null;
	            for (int count = 0; count < browsers.length && browser == null; count++)
	               if (Runtime.getRuntime().exec(
	                     new String[] {"which", browsers[count]}).waitFor() == 0)
	                  browser = browsers[count];
	            if (browser == null)
	               throw new Exception("Could not find web browser");
	            else
	               Runtime.getRuntime().exec(new String[] {browser, url});
	            }
	         }
	      catch (Exception e) {
	         JOptionPane.showMessageDialog(null, errMsg + ":\n" + e.getLocalizedMessage());
	         }
	      }

	   }
